const express = require('express');
const cors = require('cors');
const userRoutes = require('./routes/userRoutes');
const cityRoutes = require('./routes/cityRoutes');
const weatherRoutes = require('./routes/weatherRoutes');
const aiRoutes = require('./routes/aiRoutes');
const authRoutes = require('./routes/auth');
const errorHandler = require('./middleware/errorHandler');
const path = require('path');

const app = express();

app.use(cors());
app.use(express.json());

// 配置静态文件服务，用于访问上传的头像等文件
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));

// 挂载路由模块，确保路径匹配前端的实际请求
app.use('/api/auth', authRoutes);
app.use('/api/users', userRoutes); 
app.use('/api/cities', cityRoutes); 
app.use('/api/weather', weatherRoutes); 
app.use('/api/ai', aiRoutes);

// 全局错误处理中间件
app.use(errorHandler);

module.exports = app;